//从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。 
//
// 
//
// 例如: 
//给定二叉树: [3,9,20,null,null,15,7], 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回： 
//
// [3,9,20,15,7]
// 
//
// 
//
// 提示： 
//
// 
// 节点总数 <= 1000 
// 
// Related Topics 树 广度优先搜索 二叉树 
// 👍 113 👎 0


package leetcode.editor.cn1;

import com.leetcode.entity.TreeNode;

import java.util.*;

//Java：从上到下打印二叉树

/**
 * 从上到下按层打印二叉树，同一层的节点按从左到右的顺序打印，每一层打印到一行。
 * <p>
 * 例如:
 * 给定二叉树: [3,9,20,null,null,15,7],
 * <p>
 * 3
 * / \
 * 9  20
 * /  \
 * 15   7
 * <p>
 * 返回：[
 * [3],
 * [9,20],
 * [15,7]
 * ]
 */
public class CongShangDaoXiaDaYinErChaShuLcof_3 {
    public static void main(String[] args) {
        Solution solution = new CongShangDaoXiaDaYinErChaShuLcof_3().new Solution();
        // TO TEST

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
//        root.left.left = new TreeNode(15);
//        root.left.right = new TreeNode(2);
//        root.right.left = new TreeNode(6);
//        root.right.right = new TreeNode(9);
        root.left.left = new TreeNode(3);
        root.left.left.left = new TreeNode(4);
        root.left.left.left.left = new TreeNode(5);

        List<List<Integer>> list = solution.levelOrder(root);
        StringBuilder sb = new StringBuilder();
        for (List<Integer> items : list) {
            sb.append("[");
            for (Integer item : items) {
                sb.append(item + ",");
            }
            sb.append("],");
        }
        System.out.println(sb.toString());

//        int[] res = solution.levelOrder(root);
//        for (int re : res) {
//            System.out.println(re);
//        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */

    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            if (root == null)
                return new ArrayList<>();
            Queue<TreeNode> queue = new ArrayDeque<>();
            List<List<Integer>> list = new ArrayList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                List<Integer> list1 = new ArrayList<>();
                for (int i = queue.size(); i >= 1; i--) {
                    TreeNode node = queue.poll();
                    if (node != null) {
                        list1.add(node.val);
                    }
                    if (node.left != null) {
                        queue.offer(node.left);
                    }
                    if (node.right != null) {
                        queue.offer(node.right);
                    }
                }
                if (!list1.isEmpty()) {
                    list.add(list1);
                }
            }
            return list;
        }
    }


    //leetcode submit region end(Prohibit modification and deletion)

}
